package com.ctsi.ntedu.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.lang.Integer;

import com.ctsi.ntedu.domain.Overview;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctsi.ntedu.service.NteduRecordService;
import com.ctsi.ntedu.domain.NteduRecord;
import com.ctsi.ntedu.domain.NteduRecordExample;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing NteduRecord.
 *
 * @author ctsi-biyi-generator
 *
 */
@RestController
@RequestMapping("/api")
public class NteduRecordController {

    private final Logger log = LoggerFactory.getLogger(NteduRecordController.class);

    private static final String ENTITY_NAME = "nteduRecord";

    private final NteduRecordService nteduRecordService;

    public NteduRecordController(NteduRecordService nteduRecordService) {
        this.nteduRecordService = nteduRecordService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /nteduRecords : Create a new nteduRecord.
     *
     * @param nteduRecord the nteduRecord to create
     * @return the ResponseEntity with status 201 (Created) and with body the new nteduRecord, or with status 400 (Bad Request) if the nteduRecord has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/nteduRecords")
    public ResponseEntity<NteduRecord> createNteduRecord(@RequestBody NteduRecord nteduRecord) throws URISyntaxException {
        log.debug("REST request to save NteduRecord : {}", nteduRecord);
        if (nteduRecord.getId() != null) {
            throw new BadRequestAlertException("A new nteduRecord cannot already have an id", ENTITY_NAME, "idexists");
        }
        NteduRecord result = nteduRecordService.insert(nteduRecord);
        return ResponseEntity.created(new URI("/api/nteduRecords" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /nteduRecords : Updates an existing nteduRecord.
     *
     * @param nteduRecord the nteduRecord to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated nteduRecord,
     * or with status 400 (Bad Request) if the nteduRecord is not valid,
     * or with status 500 (Internal Server Error) if the nteduRecord couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/nteduRecords")
    public ResponseEntity<NteduRecord> updateNteduRecord(@RequestBody NteduRecord nteduRecord) throws URISyntaxException {
        log.debug("REST request to update NteduRecord : {}", nteduRecord);
	    if (nteduRecord.getId() == null) {
	    	return createNteduRecord(nteduRecord);
	    }
        NteduRecord result = nteduRecordService.update(nteduRecord);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
    
    /**
     * GET  /nteduRecords : get the nteduRecords with page.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduRecords in body
     */
    @GetMapping("/nteduRecordsByCriteria")
    public PageResult<NteduRecord> getNteduRecordsByCriteria(NteduRecordExample nteduRecordExample, Pageable page) {
        log.debug("REST request to get NteduRecordsByCriteria");
        return nteduRecordService.findByExample(nteduRecordExample, page);
    }
    
    /**
     * GET  /nteduRecords : get the nteduRecords.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of nteduRecords in body
     */
    @GetMapping("/nteduRecordsList")
    public PageResult<NteduRecord> getNteduRecordsList(NteduRecordExample nteduRecordExample) {
        log.debug("REST request to get NteduRecordsList");
        return nteduRecordService.findByExample(nteduRecordExample);
    }

    /**
     * GET  /nteduRecords/:id : get the "id" nteduRecord.
     *
     * @param id the id of the nteduRecord to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the nteduRecord, or with status 404 (Not Found)
     */
    @GetMapping("/nteduRecords/{id}")
    public ResponseEntity<NteduRecord> getNteduRecord(@PathVariable Integer id) {
        log.debug("REST request to get NteduRecord : {}", id);
        NteduRecord nteduRecord = nteduRecordService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(nteduRecord));
    }
	
    /**
     * DELETE  /nteduRecords/:id : delete the "id" nteduRecord.
     *
     * @param id the id of the nteduRecord to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/nteduRecords/{id}")
    public ResponseEntity<Void> deleteNteduRecord(@PathVariable Integer id) {
        log.debug("REST request to delete NteduRecord : {}", id);
        nteduRecordService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @GetMapping("/nteduRecords")
    public List<Overview> getOverview(NteduRecordExample nteduRecordExample){
        log.debug("REST request to get NteduRecordOverview");
        return nteduRecordService.selectByDate(nteduRecordExample);
    }
}
